/**
 * @Description:
 * @author 若琳
 * @date 2021/8/16
*/


const express = require('express');
const DictController = express.Router();
const DictService = require("../../../../../../../../ruoling-system/src/com/ruoling/system/service/v1/system/service.dict.js");
const CommonService = require("../../../../../../../../ruoling-system/src/com/ruoling/system/service/v1/common/service.common.js");
//数据集合
const ResultJson = require("../../../../../../../../ruoling-model/src/com/ruoling/model/SystemModel/ResultJson.js").V("v1.0.0");

const LayuiGrid = require("../../../../../../../../ruoling-model/src/com/ruoling/model/SystemModel/layuiGrid.js");


/**
 * 字典类型列表视图
 */
DictController.get("/system/dicttype/list/view",function (Request,response){

    response.type(".html");
    response.render("admin/system/sysdict/dicttypeview.html");
});

/**
 * 字典类型添加视图
 */
DictController.get("/system/dicttype/add/view",function (Request,response){

    response.type(".html");
    response.render("admin/system/sysdict/dicttypeaddview.html");
});


/**
 * 字典类型编辑视图
 */
DictController.get("/system/dicttype/edit/view",function (Request,response){

    let Id = Request.query.Id;
    DictService.DictTypeById(Id,(err,data)=>{
        response.type(".html");
        response.render("admin/system/sysdict/dicttypeeditview.html",{data:data});
    })

});


/**
 * 字典数据列表视图
 */
DictController.get("/system/dictdata/list/view",function (Request,response){

    response.type(".html");
    response.render("admin/system/sysdict/dictdataview.html");
});

/**
 * 字典数据添加视图
 */
DictController.get("/system/dictdata/add/view",function (Request,response){
    let type = Request.query.type;
    response.type(".html");
    response.render("admin/system/sysdict/dictdataaddview.html",{type:type});
});


/**
 * 字典数据编辑视图
 */
DictController.get("/system/dictdata/edit/view",function (Request,response){
    let Id = Request.query.Id;
    DictService.DictDataById(Id,(err,data)=>{
        response.type(".html");
        response.render("admin/system/sysdict/dictdataeditview.html",{data:data});
    })

});



/**
 * 数据类型列表
 */
DictController.get("/v1/api/dicttype/list",function (Request,response){

    try{
        let {limit,page,sys_dict_name} = Request.query;

        DictService.DictTypeListService(limit,page,sys_dict_name,(err,data)=>{
            if(err){
                response.send(ResultJson.GUBError());
                return;
            }
            response.send(new LayuiGrid(data.pager.totalRecord,data.pager.dataList));
            return;
        });
    }catch (e){
        response.send(ResultJson.SystemError());
        return;
    }
});

/**
 * 字典类型添加
 */
DictController.post("/v1/api/dicttype/add",function (Request,response){

    try{
        let {sys_dict_name,sys_dict_datatype,sys_dict_remark,sys_dict_status} = Request.body;

        DictService.DictTypeAddService(sys_dict_name,sys_dict_datatype,sys_dict_remark,sys_dict_status,(err,row)=>{
            if(err){
                response.send(ResultJson.GUBError());
                return;
            }
            if(row.code == 0){
                response.send(ResultJson.MegOk(row.msg));
                return;
            }else{
                response.send(ResultJson.Error(row.msg));
                return;
            }

        })
    }catch (e){
        response.send(ResultJson.SystemError());
        return;
    }
});

/**
 * 字典类型编辑
 */
DictController.post("/v1/api/dicttype/edit",function (Request,response){

    try{
        let {sys_dict_name,Id,sys_dict_remark,sys_dict_status,sys_dict_datatype} = Request.body;

        DictService.DictTypeEditService(sys_dict_name,sys_dict_remark,sys_dict_status,Id,sys_dict_datatype,(err,row)=>{
            if(err){
                response.send(ResultJson.GUBError());
                return;
            }
            if(row.code == 0){
                response.send(ResultJson.MegOk(row.msg));
                return;
            }else{
                response.send(ResultJson.Error(row.msg));
                return;
            }
        })
    }catch (e){
        response.send(ResultJson.SystemError());
        return;
    }
});


/**
 * 字典类型删除，并且把字典数据也删除
 */
DictController.post("/v1/api/dicttype/delete",function (Request,response){

    try{
        let Id = Request.body.Id;

        DictService.DictTypeDeleteService(Id,(err,row)=>{
            if(err){
                response.send(ResultJson.GUBError());
                return;
            }
            response.send(ResultJson.MegOk(""));
            return;
        })
    }catch (e){
        response.send(ResultJson.SystemError());
        return;
    }
});


/**
 * 数据类型列表
 */
DictController.get("/v1/api/dictdata/list",function (Request,response){

    try{
        let {limit,page,sys_dict_type,sys_dict_label} = Request.query;

        DictService.DictDataListService(limit,page,sys_dict_type,sys_dict_label,(err,data)=>{
            if(err){
                response.send(ResultJson.GUBError());
                return;
            }
            response.send(new LayuiGrid(data.pager.totalRecord,data.pager.dataList));
            return;
        });
    }catch (e){
        response.send(ResultJson.SystemError());
        return;
    }
});


/**
 * 字典数据添加
 */
DictController.post("/v1/api/dictdata/add",function (Request,response){

    try{
        let {sys_dict_state,sys_dict_sort,sys_dict_label,sys_dict_value,sys_dict_type,sys_dict_remark} = Request.body;
        DictService.DictDataddService(sys_dict_state,sys_dict_sort,sys_dict_label,sys_dict_value,sys_dict_type,sys_dict_remark,(err,row)=>{
            if(err){
                response.send(ResultJson.GUBError());
                return;
            }
            if(row.code == 0){
                response.send(ResultJson.MegOk(row.msg));
                return;
            }
        })
    }catch (e){
        response.send(ResultJson.SystemError());
        return;
    }
});


/**
 * 字典数据编辑
 */
DictController.post("/v1/api/dictdata/edit",function (Request,response){

    try{
        let {sys_dict_state,sys_dict_sort,sys_dict_label,sys_dict_value,sys_dict_type,sys_dict_remark,Id} = Request.body;

        DictService.DictDataEditService(sys_dict_state,sys_dict_sort,sys_dict_label,sys_dict_value,sys_dict_type,sys_dict_remark,Id,(err,row)=>{
            if(err){
                response.send(ResultJson.GUBError());
                return;
            }
            if(row.code == 0){
                response.send(ResultJson.MegOk(row.msg));
                return;
            }else{
                response.send(ResultJson.Error(row.msg));
                return;
            }
        })
    }catch (e){
        response.send(ResultJson.SystemError());
        return;
    }
});


/**
 * 字典数据删除
 */
DictController.post("/v1/api/dictdata/delete",function (Request,response){

    try{
        let Id = Request.body.Id;
        CommonService.Common_Delete_All("rl_sys_dict_data",Id,(err,row)=>{
            if(err){
                response.send(ResultJson.GUBError());
                return;
            }
            response.send(ResultJson.MegOk(""));
            return;
        })
    }catch (e){
        response.send(ResultJson.SystemError());
        return;
    }
});





module.exports = DictController;